package org.cnbi.web.controller.manage;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.tools.generic.NumberTool;
import org.cnbi.entity.Menu;
import org.cnbi.service.IHandleService;
import org.cnbi.service.IMenuService;
import org.cnbi.service.ISystemService;
import org.cnbi.utils.json.JsonUtil;
import org.cnbi.utils.pojo.MsgBean;
import org.cnbi.utils.pojo.SqlParamsBean;
import org.cnbi.utils.tools.ActionConstants;
import org.cnbi.utils.tools.Constants;
import org.cnbi.utils.tools.GlobalConstants;
import org.cnbi.utils.tools.MenuConstants;
import org.cnbi.web.controller.base.BaseController;
import org.cnbi.web.utils.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 菜单管理 menuM Controller
 * Copyright © 2014中国经邦. All rights reserved.
 * @author zhh
 * @date 2014年5月9日下午13:51
 * @cnbi-web
 */
@Controller
@RequestMapping("/menuM")
public class MenuMController extends BaseController {
	
	private static final Log logger = LogFactory.getLog(MenuMController.class);
	
	public static final NumberTool numTools = new NumberTool();
	
	@Autowired
	private IHandleService handleService;
	
	@Autowired
	private IMenuService menuService ;
	
	@Autowired
	private ISystemService systemService;
	/**
	 * 菜单管理 menu 入口
	 * @param param 参数类
	 * @param menu 当前 菜单项
	 * **/
	@RequestMapping(value = "/index")
	public ModelAndView main(SqlParamsBean param,Menu menu,HttpServletResponse response) {
		Menu menus = (Menu) ContextHolderUtils.getServletContext().getAttribute(MenuConstants.MENU+MenuConstants.TREE);
		List<Menu> list = new ArrayList<Menu>();
		list.add(menus);
		//@SuppressWarnings("unchecked")
	//	List<Menu> menus = (List<Menu>) ContextHolderUtils.getServletContext().getAttribute(ActionConstants.INIT+MenuConstants.MENU);
		ModelAndView view = returnView(menu,response,list);
		return view;
	}
	
	/**
	 * 修改菜单
	 * @param menu
	 * @return view 
	 * **/
	@RequestMapping(value = "/updateMenu")
	public ModelAndView updateMenu(Menu menu,HttpServletResponse response){
		editMenu(menu);
		ModelAndView view = returnView(menu,response,null);
		MsgBean msg = new MsgBean(true, menu.getProperty1());
		view.addObject(Constants.MSGBEAN, msg);
		return view;
	}

	/**
	 * 查询菜单  返回 ModelAndView
	 * @param menu 菜单
	 * @return view
	 * **/
	private ModelAndView returnView(Menu menu,HttpServletResponse response, List<Menu> menus) {
		if(menus == null){
			String sql = getSqlByKey(ActionConstants.INIT+MenuConstants.MENU+MenuConstants.TREE);
			Menu menux=  (Menu) handleService.queryMenuTree(sql, new Menu("0","所有菜单"));
			menus = new ArrayList<Menu>();
			menus.add(menux);
		}
		logger.info("datas===>>>>"+JsonUtil.generateJson(menus));
		ModelAndView view = secondNavHandle(menu, response);
		view.addObject("pageBean", menu);
		setContentType(response, null);
		setContext(view);
		view.addObject(Constants.DATAS, menus);
		return view;
	}

	/**
	 * 修改菜单
	 * @param menu 菜单
	 * */
	private void editMenu(Menu menu) {
		String sql = getSqlByKey(MenuConstants.MENU+ActionConstants.UPDATE);
		menuService.editMenu(sql, menu);
		systemService.addLog(getSqlByKey(ActionConstants.MANAGE + ActionConstants.ADD+"Log"),getSysLog("菜单【" + menu.getSname()+ "】修改成功！", GlobalConstants.Log_Type_INSERT, GlobalConstants.Log_Leavel_INFO));

	}
	
	
	
}
